package com.shenzhen.horselive.ui.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.flqy.baselibrary.utils.L;
import com.shenzhen.horselive.R;
import com.shenzhen.horselive.User;
import com.shenzhen.horselive.adapter.AddrListAdpter;
import com.shenzhen.horselive.api.ApiFactory;
import com.shenzhen.horselive.base.AppBarActivity;
import com.shenzhen.horselive.beans.AddrListBean;
import com.shenzhen.horselive.beans.AddressBean;
import com.shenzhen.horselive.databinding.ActivityAddrListBinding;
import com.shenzhen.horselive.network.SchedulersCompat;
import com.shenzhen.horselive.presenter.LoadingViewObserver;

public class AddrListActivity extends AppBarActivity {
  private  final  int  REQUEST_CODE = 1000;
    ActivityAddrListBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding =  ActivityAddrListBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        inititViews();
        setStyleDark();
        setTitle("收货地址管理");
        setMenuIcon(R.mipmap.ico_add);
        initData();
    }

    private void initData() {
        ApiFactory.getApiService().getAddressList(User.get().getMyInfo().getToken())
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<AddrListBean>(this) {
                    @Override
                    protected void onNextInActive(AddrListBean addrListBean) {
                        if (addrListBean.isSuccess())
                        {
                            adpter.setNewData(addrListBean.getData());
                        }else
                        {
                            showToast(addrListBean.getErrMessage());
                        }
                    }
                });
    }

    AddrListAdpter  adpter;
    private void inititViews() {
        binding.recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
        adpter = new AddrListAdpter(R.layout.item_addrs,null);
        binding.recyclerview.setAdapter(adpter);
        adpter.setEmptyView(R.layout.no_data_layout);
        adpter.addChildClickViewIds(R.id.img_ed_addr);
        adpter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                //编辑地址
                if (getIntent().getBooleanExtra("iscall",false))
                {
                    Intent starter = new Intent();
                    starter.putExtra("data",(AddressBean) adapter.getData().get(position));
                    setResult(1001,starter);
                    finish();
                }

            }
        });

        adpter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                switch (view.getId())
                {
                    case R.id.img_ed_addr:
                        //编辑地址
                        Intent starter = new Intent(AddrListActivity.this, AddAddrActivity.class);
                        starter.putExtra("type",1);
                        starter.putExtra("data",(AddressBean) adapter.getData().get(position));
                        startActivityForResult(starter,REQUEST_CODE);
                        break;
                }
            }
        });

    }

    @Override
    protected void onActionMenuViewClick() {
        super.onActionMenuViewClick();
        //添加地址
        Intent starter = new Intent(this, AddAddrActivity.class);
        starter.putExtra("type",0);
        startActivityForResult(starter,REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (REQUEST_CODE==requestCode)
        {
            //刷新列表
            initData();
        }
    }
}